﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class CourseEditBySchoolAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null || ((Entity.UserInfo)Session["UserInfo"]).UserType != "School")
            {
                Response.Redirect("~/Search.aspx");
            }

            if (!IsPostBack)
            {
                if (Session["SchoolId"] == null)
                {
                    Response.Redirect("WelcomeSchoolAccount.aspx");
                }
                //configrate treeview nodes
                tvTag.Nodes.Clear();
                TreeNode rootNode = new TreeNode();
                TreeNode childNode = new TreeNode();
                TreeNode leafNode = new TreeNode();
                DataTable dtRootTags = new DataTable();
                DataTable dtChildTags = new DataTable();
                DataTable dtLeafTags = new DataTable();

                SQLHelper.getRootTags(out dtRootTags);
                for (int ix = 0; ix < dtRootTags.Rows.Count; ix++)
                {
                    rootNode = new TreeNode(dtRootTags.Rows[ix]["TagName"].ToString(), dtRootTags.Rows[ix]["Id"].ToString());
                    rootNode.SelectAction = TreeNodeSelectAction.None;
                    tvTag.Nodes.Add(rootNode);
                    SQLHelper.getChildTagsByParentId(Convert.ToInt32(rootNode.Value), out dtChildTags);
                    for (int iy = 0; iy < dtChildTags.Rows.Count; iy++)
                    {
                        childNode = new TreeNode(dtChildTags.Rows[iy]["TagName"].ToString(), dtChildTags.Rows[iy]["Id"].ToString());
                        childNode.SelectAction = TreeNodeSelectAction.None;
                        tvTag.Nodes[ix].ChildNodes.Add(childNode);
                        SQLHelper.getChildTagsByParentId(Convert.ToInt32(childNode.Value), out dtLeafTags);
                        for (int iz = 0; iz < dtLeafTags.Rows.Count; iz++)
                        {
                            leafNode = new TreeNode(dtLeafTags.Rows[iz]["TagName"].ToString(), dtLeafTags.Rows[iz]["Id"].ToString());
                            leafNode.SelectAction = TreeNodeSelectAction.None;
                            tvTag.Nodes[ix].ChildNodes[iy].ChildNodes.Add(leafNode);
                        }
                    }

                }

                if (Session["CourseId"] == null)
                {

                }
                else
                {
                    int iCourseId = Convert.ToInt32(Session["CourseId"]);
                    DataTable dt = new DataTable();
                    SQLHelper.getCourseById(iCourseId, out dt);
                    tbxCourseName.Text = dt.Rows[0]["CourseName"].ToString();
                    tbxCourseCapability.Text = dt.Rows[0]["CourseCapability"].ToString();
                    tbxCoursePrice.Text = dt.Rows[0]["CoursePrice"].ToString();
                    tbxAddress.Text = dt.Rows[0]["Address"].ToString();
                    string strPK_TagInfo = dt.Rows[0]["PK_TagInfo"].ToString();

                    Session.Add("CheckedTagId", dt.Rows[0]["PK_TagInfo"]);
                    foreach (TreeNode m_rootNode in tvTag.Nodes)
                    {
                        if (strPK_TagInfo == m_rootNode.Value.ToString())
                        {
                            m_rootNode.Checked = true;
                            break;
                        }
                        foreach (TreeNode m_childNode in m_rootNode.ChildNodes)
                        {
                            if (strPK_TagInfo == m_childNode.Value.ToString())
                            {
                                m_childNode.Checked = true;
                                break;
                            }
                            foreach (TreeNode m_leafNode in m_childNode.ChildNodes)
                            {
                                if (strPK_TagInfo == m_leafNode.Value.ToString())
                                {
                                    m_leafNode.Checked = true;
                                    break;
                                }
                            }
                        }
                    }



                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (tbxCourseName.Text == "")
            {
                Response.Write("<script language = javascript>alert('请输入课程名字');</script>");
                return;
            }
            if (tbxCourseCapability.Text == "")
            {
                Response.Write("<script language = javascript>alert('请输入课程人数');</script>");
                return;
            }
            if (tbxCoursePrice.Text == "")
            {
                Response.Write("<script language = javascript>alert('请输入课程价格');</script>");
                return;
            }
            if (tbxAddress.Text == "")
            {
                Response.Write("<script language = javascript>alert('请输入上课地址');</script>");
                return;
            }
            if (Session["CheckedTagId"] == null)
            {
                Response.Write("<script language = javascript>alert('请选择课程所属标签');</script>");
                return;
            }


            if (Session["CourseId"] != null)
            {
                int iCourseId = (int)Session["CourseId"];
                try
                {
                    int iresult = SQLHelper.updateCourseById(iCourseId, tbxCourseName.Text, (int)Session["CheckedTagId"], Convert.ToInt32(tbxCourseCapability.Text), Convert.ToInt32(tbxCoursePrice.Text), tbxAddress.Text);
                    if (iresult == 1)
                    {
                        Session.Remove("CourseId");
                        Response.Write("<script>alert('编辑成功!');location.href='CourseManage.aspx';</script>");
                    }
                    else
                    {
                        Session.Remove("CourseId");
                        Response.Write("<script>alert('编辑成功!');location.href='CourseManage.aspx';</script>");
                    }
                }
                catch (Exception ex)
                {
                    Session.Remove("CourseId");
                    Response.Write("<script>alert('" + ex.Message + "');location.href='CourseManage.aspx';</script>");
                }


            }
            else
            {
                try
                {
                    int iresult = SQLHelper.AddCourse(tbxCourseName.Text, (int)Session["CheckedTagId"], Convert.ToInt32(tbxCourseCapability.Text), Convert.ToInt32(tbxCoursePrice.Text), tbxAddress.Text, (int)Session["SchoolId"]);
                    if (iresult == 1)
                    {
                        Session.Remove("CourseId");
                        Response.Write("<script>alert('新增成功!');location.href='CourseManage.aspx';</script>");
                    }
                    else
                    {
                        Session.Remove("CourseId");
                        Response.Write("<script>alert('新增失败!');location.href='CourseManage.aspx';</script>");
                    }
                }
                catch (Exception ex)
                {
                    Session.Remove("CourseId");
                    Response.Write("<script>alert('" + ex.Message + "');location.href='CourseManage.aspx';</script>");
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Session.Remove("SchoolId");
            Response.Redirect("SchoolManage.aspx");
        }

        protected void tvTag_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            if (tvTag.CheckedNodes.Count > 0)
            {
                foreach (TreeNode tnRoot in tvTag.Nodes)
                {
                    if (tnRoot.Checked == false)
                        tnRoot.ShowCheckBox = false;
                    else
                        Session["CheckedTagId"] = Convert.ToInt32(tnRoot.Value);
                    foreach (TreeNode tnSecond in tnRoot.ChildNodes)
                    {
                        if (tnSecond.Checked == false)
                            tnSecond.ShowCheckBox = false;
                        else
                            Session["CheckedTagId"] = Convert.ToInt32(tnSecond.Value);
                        foreach (TreeNode tnLeaf in tnSecond.ChildNodes)
                        {
                            if (tnLeaf.Checked == false)
                                tnLeaf.ShowCheckBox = false;
                            else
                                Session["CheckedTagId"] = Convert.ToInt32(tnLeaf.Value);
                        }

                    }
                }
            }
            else
            {
                Session.Remove("CheckedTagId");
                foreach (TreeNode tnRoot in tvTag.Nodes)
                {
                    tnRoot.ShowCheckBox = true;
                    foreach (TreeNode tnSecond in tnRoot.ChildNodes)
                    {
                        tnSecond.ShowCheckBox = true;
                        foreach (TreeNode tnLeaf in tnSecond.ChildNodes)
                        {
                            tnLeaf.ShowCheckBox = true;
                        }

                    }
                }
            }

        }
    }
}